package com.mew.slots.core.game.jdqe.room;

import com.mew.slots.core.common.game.BaseRoom;
import com.mew.slots.core.game.jdqe.roller.Controlline;
import com.mew.slots.core.game.jdqe.bo.JdqePlayMsgs;
import com.mew.slots.core.game.jdqe.roller.JdqeIli;

import java.util.ArrayList;
import java.util.Arrays;

/**
 * @author dylin
 */
public class JdqeRoom extends BaseRoom {

    public static int _SCATTER = 11;
    public static int _BONUS = 12;
    public static int _JP = 13;

    public JdqeRoom(){
        res = new JdqePlayMsgs();
        row = 3;
        col = 5;
        _WILD = 10;
        spes = new ArrayList<Integer>(){{
            add(11);
            add(12);
        }};

         rate = new int[][]{
                 {0, 0,  5, 15, 50},      //10            0
                 {0, 0,  5, 15, 50},      //J             1
                 {0, 0,  5, 15, 50},      //Q             2
                 {0, 0,  5, 15, 50},      //K             3
                 {0, 0,  5, 15, 50},      //A             4

                 {0, 0, 10, 25, 100},      //冰屋          5

                 {0, 0, 10, 30, 150},      //🐢            6
                 {0, 0, 10, 30, 150},      //海豹           7
                 {0, 0, 10, 30, 150},      //🐬             8

                 {0, 5, 10, 30, 150},      //🐧             9

                 {0, 0,  0,   0,  0},      //WILD           10
                 {0, 0,  0,   0,  0},      //SCATTER        11
                 {0, 0,  0,   0,  0},      //Bonus          12
                 {0, 0,  0,   0,  0},      //JACK           13
        };

         alllines = new int[][]{
                 {5,6,7,8,9},
                 {0,1,2,3,4},
                 {10,11,12,13,14},
                 {0,6,12,8,4},
                 {10,6,2,8,14},
                 {5,1,7,13,9},
                 {5,11,7,3,9},
                 {0,1,7,13,14},
                 {10,11,7,3,4},
                 {0,6,2,8,4},
                 {10,6,12,8,14},
                 {5,1,2,3,9},
                 {5,11,12,13,9},
                 {0,6,7,8,4},
                 {10,6,7,8,14},
                 {5,6,2,8,9},
                 {5,6,12,8,9},
                 {5,1,7,3,9},
                 {5,11,7,13,9},
                 {0,1,2,8,14},
                 {10,11,12,8,4},
                 {0,6,12,13,14},
                 {10,6,2,3,4},
                 {5,6,7,3,14},
                 {5,6,7,13,4},
                 {0,6,7,8,14},
                 {10,6,7,8,4},
                 {10,11,7,3,9},
                 {0,1,7,13,9},
                 {10,6,12,13,14},
                 {0,6,2,3,4},
                 {0,6,12,8,9},
                 {10,6,2,8,9},
                 {5,1,7,3,4},
                 {5,11,7,13,14},
                 {0,6,7,8,9},
                 {10,6,7,8,9},
                 {5,1,2,3,4},
                 {5,11,12,13,14},
                 {5,6,7,8,4},
                 {5,6,7,8,14},
                 {0,1,7,3,4},
                 {10,11,7,13,14},
                 {0,1,2,8,4},
                 {10,11,12,8,14},
                 {5,6,2,3,4},
                 {5,6,12,13,14},
                 {0,1,7,8,4},
                 {10,11,7,8,14},
                 {10,6,2,8,4}
         };

    }
    @Override
    public void getLinesXml(){
        if (controlType>0){
            endSlots = control();
        } else {
            endSlots = rollerXml.getLineInts( free ? 1 : 0,rollerID);
        }
        res.endSlots = endSlots.clone();
        if(free){
            computeWildBonus();
        }
    }
    @Override
    public void getLinesIli(){
        int[] rs;
        if (controlType>0){
            rs = control();
        } else {
            rs = getIliEndSlots();
        }
        endSlots =  Arrays.copyOf(rs,col*row);
        res.endSlots  = endSlots.clone();
        if(free){
            computeWildBonus();
        }
    }

    public int[] getIliEndSlots(){
        if(free){
            return iliXml.getSpeints("mfree");
        }else{
            return iliXml.getLineints(0,rollerID,in,0);
        }
    }

    private void computeWildBonus(){
        JdqePlayMsgs pmres=(JdqePlayMsgs) res;
        c:for (int c = 0; c < col; c++) {
            r:for (int r = 0; r < row; r++) {
                if(endSlots[r*col+c] == _WILD){
                    pmres.getWns()[c] = 1;
                    endSlots[c] = _WILD;
                    endSlots[col+c] = _WILD;
                    endSlots[col*2+c] = _WILD;
                    continue c;
                }
            }
        }
    }
    //ili
    private void computeIliWildBonus(int[] ws){
        JdqePlayMsgs pmres=(JdqePlayMsgs) res;
        for (int i = 0; i < ws.length; i++) {
            if(ws[i] == 1){
                pmres.getWns()[i] = 1;
                endSlots[i] = _WILD;
                endSlots[col+i] = _WILD;
                endSlots[col*2+i] = _WILD;
            }
        }
    }

    @Override
    public void freeBouns() {
        int cNum = fsdb(_SCATTER);
        if(cNum > 2){
            res.setFree((byte) 1);
            res.setFreeNumber(10);
        }
        int bNum = fsdb(_BONUS);
        if(bNum > 2){
            res.setBouns(1);
        }
        int jNum = fsdb(_JP);
        if(jNum > 2){
            res.setBouns(2);
        }
    }

    @Override
    public int[] control(){
        JdqeIli instance = JdqeIli.getInstance();
        switch (controlType) {
            case 1:
                return bigwin();
            case 2:
                return instance.getSpeints("free");
            case 3:
                return instance.getSpeints("bonus");
            case 4:
                return instance.getSpeints("jp");
            default:
                return instance.getSpeints("0-0");
        }
    }
}
